package com.example.hfSystem.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.hfSystem.annotation.IgnoreAuth;
import com.example.hfSystem.entity.pojo.Config;
import com.example.hfSystem.exception.EIException;
import com.example.hfSystem.service.ConfigService;
import com.example.hfSystem.utils.R;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author zx
 * @version 1.0
 * @package_name: com.example.hfSystem.controller
 * @date 2024/4/9 15:56
 * 上传文件映射表
 */

@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private ConfigService configService;
    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    @IgnoreAuth
    public R upload(@RequestParam("file") MultipartFile file, String type) throws Exception {
        if (file.isEmpty()) {
            throw new EIException("上传文件不能为空");
        }
        String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        File path = new File(ResourceUtils.getURL("classpath:static").getPath());
        if(!path.exists()) {
            path = new File("");
        }
        File upload = new File(path.getAbsolutePath(),"/upload/");
        if(!upload.exists()) {
            upload.mkdirs();
        }
        String fileName = new Date().getTime()+"."+fileExt;
        File dest = new File(upload.getAbsolutePath()+"/"+fileName);
        file.transferTo(dest);

        if(StringUtils.isNotBlank(type) && type.equals("1")) {
            Config configEntity = configService.selectOne(new EntityWrapper<Config>().eq("name", "faceFile"));
            if(configEntity==null) {
                configEntity = new Config();
                configEntity.setName("faceFile");
                configEntity.setValue(fileName);
            } else {
                configEntity.setValue(fileName);
            }
            configService.insertOrUpdate(configEntity);
        }
        return R.ok().put("file", fileName);
    }

    /**
     * 下载文件
     */
    @IgnoreAuth
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam String fileName) {
        try {
            File path = new File(ResourceUtils.getURL("classpath:static").getPath());
            if(!path.exists()) {
                path = new File("");
            }
            File upload = new File(path.getAbsolutePath(),"/upload/");
            if(!upload.exists()) {
                upload.mkdirs();
            }
            File file = new File(upload.getAbsolutePath()+"/"+fileName);
            if(file.exists()){
				/*if(!fileService.canRead(file, SessionManager.getSessionUser())){
					getResponse().sendError(403);
				}*/
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setContentDispositionFormData("attachment", fileName);
                return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
